The first instance of obj whose box overlaps the rectangle from (x1,y1) to (x2,y2), or nothing.
collision_rect(x1, y1, x2, y2, obj) -> ref<object>| Name | Type |
|---|---|
x1 | real |
y1 | real |
x2 | real |
y2 | real |
obj | object |
ref<object>
The first instance of obj whose box overlaps the rectangle from (x1, y1) to (x2, y2), or nothing. A broad-phase query for "what's in this area" - an explosion's blast radius, a swept melee hit - rather than a test against one instance or one point.
Both corners, (x1, y1) and (x2, y2), are places in the room counted from the room's top-left corner.
Only the first matching instance is returned; a rectangle that could contain several does not report the rest.
If obj is a parent, the answer can be one of its children. With obj_enemy as the parent of obj_slime, collision_rect(x1, y1, x2, y2, obj_enemy) can hand back a slime. The reference is typed as obj_enemy, so you can use the variables and events obj_enemy has through it. The parent's own instances are searched first, then each child's. An object whose Code-tab box Never moves is ticked is answered from a look-up grid instead - same answer, much less work when there are many copies (Never moves). It still hands back the same copy: the first one, in the same order.
An instance destroyed earlier in the same frame still counts for this check until the end of the frame, because instance_destroy only ends it then. The one exception is a Collision event, which skips it at once.
An instance with no sprite and no mask size is a single point. A box meets that point only when the point is strictly inside it - past its left and top edges and short of its right and bottom edges. A point exactly on an edge or a corner is not met, and two boxes with no size never meet. (Overlap and collision has the whole rule.) A marker with no size placed inside the rectangle is found; one exactly on its edge is not.
var hit = collision_rect(x - 24, y - 24, x + 24, y + 24, obj_doc_enemy);
if (hit != nothing) {
instance_destroy(hit);
}The instant this spark appears, destroys the first obj_doc_enemy whose box overlaps a 48x48 square centered on it - a single blast-radius query, not a loop over every enemy in the room.
If obj is a parent, a child instance can be the answer: the parent’s own pool is searched first, then each descendant’s, and the result is typed as obj. The FIRST one, never a list - and it keeps returning an instance destroyed earlier in this same frame, because a destroyed slot is only freed at the end of the frame (see instance_destroy). Those two together mean "hit everything inside this box" cannot be written from the box’s side at all: calling this in a loop and destroying what it returns never ends. Turn it around and sweep from the instance side - `with (obj_enemy) { if (x > x1 && x < x2 && y > y1 && y < y2) { ... } }` reaches every match, exactly once. An instance with no sprite and no mask size is a single point at its x and y (moved by mask_x and mask_y). A box meets that point only when the point lies strictly inside it, past its left and top edges and short of its right and bottom edges; a point exactly on an edge or a corner is not met, and two boxes with no size never meet, even in the same spot. A box with a width and no height (or the reverse) is a line, met when it lies strictly between the edges of the other box across its empty side. This is what finds a point-sized marker placed inside the rectangle; one on the edge of the rectangle is not found.